import { setToken, getToken, removeToken } from '@/utils/cookie'
import { signIn, getInfo, logOut } from '@/api/register'
const user = {
  state: {
    userId: '',
    userInfo: ''
  },
  mutations: {
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
    }
  },
  actions: {
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        console.log(userInfo)
        signIn(userInfo).then(response => {
          commit('SET_USERID', response.data.result.userId)
          setToken(response.data.result.userId)
          resolve()
        })
      })
    },
    // 获取用户详细信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        const data = {
          userId: getToken()
        }
        getInfo(data).then(response => {
          resolve(response.data)
          commit('SET_USERINFO', response.data.result)
        })
      })
    },
    // 登出
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        logOut().then(response => {
          console.log(response)
          removeToken()
          commit('SET_USERINFO', '')
          commit('SET_USERID', '')
          resolve()
        })
      })
    }
  }
}
export default user
